<template>
  <view class="wrap">
    <u-form class="form" :model="model" :rules="rules" ref="uForm" label-position="left">
      <u-form-item label="名称" prop="publicName" label-width="180">
        <u-input v-model="model.publicName" placeholder="publicName"></u-input>
      </u-form-item>
      <u-form-item label="键" prop="publicKey" label-width="180">
        <u-input v-model="model.publicKey" placeholder="publicKey"></u-input>
      </u-form-item>
      <u-form-item label="值" prop="publicValue" label-width="180">
        <u-input v-model="model.publicValue" placeholder="publicValue"></u-input>
      </u-form-item>
      <u-form-item label="状态" prop="status" label-width="180">
        <js-radio v-model="model.status" itemLabel="label" itemValue="value"
                  :items="lockData"></js-radio>
      </u-form-item>
      <u-form-item label="编码" prop="validateCode" label-width="180">
        <u-input v-model="model.validateCode" placeholder="validateCode"></u-input>
      </u-form-item>
      <u-form-item label="类型" prop="publicType" label-width="180">
        <js-select v-model="model.publicType" placeholder="请选择类型" itemLabel="label" itemValue="value"
                   dictType="param_type" ></js-select>
      </u-form-item>
      <u-form-item label="内置" prop="systemFlag" label-width="180">
        <js-radio v-model="model.systemFlag" itemLabel="label" itemValue="value"
                  :items="systemData"></js-radio>
      </u-form-item>
    </u-form>
    <view class="form-footer">
      <u-button class="btn" type="primary" @click="submit">提交</u-button>
      <u-button class="btn" type="default" @click="cancel">关闭</u-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      type: '',
      model: {
        publicId: 0,
        publicName: '',
        publicKey: '',
        publicValue: '',
        status: '',
        validateCode: '',
        createBy: '',
        updateBy: '',
        createTime: '',
        updateTime: '',
        publicType: '',
        systemFlag: '',
        delFlag: '',
        tenantId: '',
      },
      lockData: [{
        label: '有效',
        value: '0'
      }, {
        label: '锁定',
        value: '9'
      }],
      systemData: [{
        label: '否',
        value: '0'
      }, {
        label: '是',
        value: '1'
      }],
      rules: {
        publicName: [
          {required: true, message: 'publicName不能为空', trigger: 'blur'}
        ],

        publicKey: [
          {required: true, message: 'publicKey不能为空', trigger: 'blur'}
        ],

        publicValue: [
          {required: true, message: 'publicValue不能为空', trigger: 'blur'}
        ],

        status: [
          {required: true, message: 'status不能为空', trigger: 'blur'}
        ],

        validateCode: [
          {required: true, message: 'validateCode不能为空', trigger: 'blur'}
        ],

        createBy: [
          {required: true, message: '创建人不能为空', trigger: 'blur'}
        ],

        updateBy: [
          {required: true, message: '修改人不能为空', trigger: 'blur'}
        ],

        createTime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],

        updateTime: [
          {required: true, message: '修改时间不能为空', trigger: 'blur'}
        ],

        publicType: [
          {required: true, message: 'publicType不能为空', trigger: 'blur'}
        ],

        systemFlag: [
          {required: true, message: 'systemFlag不能为空', trigger: 'blur'}
        ],

        delFlag: [
          {required: true, message: 'delFlag不能为空', trigger: 'blur'}
        ],

        tenantId: [
          {required: true, message: '租户ID不能为空', trigger: 'blur'}
        ],

      }
    }
  },
  onLoad(params) {
    if (params && params.id !== "undefined") {
      this.type = 'edit'
      this.$u.api.unixParam.getObj(params).then(res => {
        this.model = res.data;
      });
    } else {
      this.type = 'add'
      this.model = {}
    }
  },
  methods: {
    submit() {
      this.$refs.uForm.validate(async valid => {
        if (valid) {
          if (this.type === 'edit') {
            await this.$u.api.unixParam.putObj(this.model)
          } else {
            await this.$u.api.unixParam.addObj(this.model)
          }
          setTimeout(() => {
            uni.showModal({
              title: '提示',
              content: this.type === 'edit' ? '修改成功' : '添加成功',
              showCancel: false,
              success: function () {
                uni.setStorageSync('refreshList', true);
                uni.navigateTo({
                  url: '/pages/sys/unixParam/index'
                })
              }
            })
          }, 200)
        } else {
          this.$u.toast('您填写的信息有误，请根据提示修正。')
        }
      });
    },
    cancel() {
      uni.navigateBack();
    }
  }
}
</script>

